package yxy.game.pm2.command.godBeast;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.therion;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedGodBeastMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.godBeast.GodBeast;
import yxy.game.pm2.module.godBeast.PickedGodBeastHandle;
import yxy.game.pm2.module.godBeast.PickedGodBeastModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

import java.util.Collection;

/**
 * @name CMD_PickedGodBeastLevelUp
 * @author wrs
 * @descrition 神兽升级
 * @since 2022-05-31
 */
@MessageType(proto = Command.PickedGodBeastLevelUp,classtype = PickedGodBeastMessage.PickedGodBeastLevelUpRequest.class)
public class CMD_PickedGodBeastLevelUp extends Command<PickedGodBeastMessage.PickedGodBeastLevelUpRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastLevelUpRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int code = request.getCode(); //神兽ID

        PickedGodBeastMessage.PickedGodBeastLevelUpResponse.Builder response = PickedGodBeastMessage.PickedGodBeastLevelUpResponse.newBuilder();
        response.setCode(code);

        try {
            PickedGodBeastModule pgbModule = PickedGodBeastModule.get(playerCode);
            if (pgbModule == null) {
                send(messageSource, response, Result.DataError);//不存在,参数错误
                return;
            }

            GodBeast godBeast = new PickedGodBeastHandle().getGodBeastById(pgbModule,code);
            if (godBeast == null) {
                send(messageSource, response, Result.DataError);//参数错误
                return;
            }

            if (godBeast.getLevel() == godBeast.getMaxLevel()){
                send(messageSource,response, Result.GodBeastLevelUpper); //神兽等级达到上限
                return;
            }

            CurrencyHandle currencyHandle = new CurrencyHandle();
            therion therion = DataCenter.getData(godBeast.getCode(), yxy.cherry.data.bean.therion.class);
            Collection<Currency> currencies = Currency.create(therion.getCosttype(), therion.getCostid(), therion.getCostnum());
            boolean costCheck = currencyHandle.costCheck(playerCode, currencies);
            if (!costCheck){
                send(messageSource, response, Result.CostFail);//消耗的材料不足
                return;
            }

            currencyHandle.cost(playerCode, CCReason.神兽,currencies);
            new PickedGodBeastHandle().godBeastLevelUp(playerCode,godBeast,response);

            send(messageSource,response, Result.Success);

            PlayTypeModule playTypeModule = PlayTypeModule.get(playerCode);
            PlayTypeHandle.savePickedGodBeast(playerCode,playTypeModule);
            new BattleHandle().powerUpdate(playerCode);// 战力更新

        }catch (Exception e){
            e.printStackTrace();
            send(messageSource,response, Result.DataError);
        }

    }

    private void send(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastLevelUpResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
